//index.js
//获取应用实例
var app = getApp()
Page({
	data: {
		root:'',
		siteroot:app.setting.siteroot,
		att_url:app.setting.att_url,
		action:"order.list/",
		account:app.setting.account,
		pagetitle:'订单管理' + '-' + app.setting.title,
		loading:true,
		page:1,
		keyword:'',
		op:''
	},
	onReady: function (){
	},
	onPullDownRefresh: function (){
		var the=this;
		var opt={};
		the.onLoad(opt);
		setTimeout(function(){
			wx.stopPullDownRefresh()
		},1000);
	},
	onLoad: function (){
		var page=this;
		wx.setNavigationBarTitle({'title':page.data.pagetitle});
		app.util.get(app.access,function(pagedata){
			console.log('order_list');console.log(pagedata);
			this.data.op = pagedata.op
			pagedata.post=pagedata.post?pagedata.post:{}
			page.setData({
				pagedata:pagedata,
			});
			page.getlist(1);
		});
	},
	cancel: function (e) {
		var pagedata=this.data.pagedata
		var id = e.currentTarget.dataset.id;
		console.log('12121');console.log(id);
		app.util.post('cancel', { 'id': id });		
	},
	receive: function (e) {
		var pagedata=this.data.pagedata
		var id = e.currentTarget.dataset.id;
		app.util.post('receive',{'id':id});
	},
	keyword:function(e){
		var the = this
		var pagedata = the.data.pagedata
		pagedata.params.keyword = e.detail.value
	},
	getlist:function(e){
		var the=this
		var page=the.data.page
		var pagedata=the.data.pagedata
		var isnodata = the.data.isnodata
		var data={}
		the.setData({
			loading:true,
		});
		if(e){
			if(e.currentTarget){
				data = e.currentTarget.dataset;
				pagedata.params.status=data.status
			}
			pagedata.list=[]
			page=1
			isnodata=false;
		}else{
			if(isnodata){
				the.setData({
					loading:false,
				});
				return false;
			}
		}
		var userdeleted=data.userdeleted?data.userdeleted:'';
		app.util.get('jy_weishop/store.manage.order/getlist/status='+pagedata.params.status+'&userdeleted='+userdeleted+'&page='+page,function(res){
			if(res.list.length==0){
				the.setData({
					isnodata:true,
					'pagedata.list':pagedata.list,
					'pagedata.params.status':pagedata.params.status,
					'pagedata.userdeleted':userdeleted,
					loading:false,
				});
			}else{
				console.log(page);
				pagedata.list = pagedata.list.concat(res.list)
				page++
				the.setData({
					'pagedata.list':pagedata.list,
					'pagedata.params.status':pagedata.params.status,
					'pagedata.userdeleted':userdeleted,
					page:page,
					isnodata:isnodata,
					loading:false,
				});
			}
		});
	},
	//翻页
	onReachBottom: function () {
		var isnodata=this.data.isnodata
		var loading=this.data.loading
		if(!isnodata && !loading){
			this.getlist()
		}
	},
	ispay: function (e) {
		var data = e.currentTarget.dataset;
		app.util.get('jy_weishop/store.manage.order/ispay/id='+data.id);
	},
	shou: function (e) {
		var data = e.currentTarget.dataset;
		app.util.get('jy_weishop/store.manage.order/shou/id='+data.id);
	},
	fahuo: function (e) {
		var the=this
		var data = e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		app.util.get('jy_weishop/store.manage.order/fa/id='+data.id,function(res){
			console.log('fa');console.log(res);
			res.express=Object.values(res.express);
			pagedata.post.index=data.index
			pagedata.post.id=data.id
			the.setData({
				'pagedata.item':res.item,
				'pagedata.express':res.express,
				'pagedata.post':pagedata.post,
				showfahuo:true,
			});
		});
	},
	set: function (e) {
		var the=this
		var data = e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		app.util.post('jy_weishop/store.manage.order/postfa/postdata=1',pagedata.post,function(res){
			pagedata.list[pagedata.post.index].status=2;
			the.setData({
				'pagedata.list':pagedata.list,
				showfahuo:false
			});
			app.util.msg('发货成功！');
		});
	},
	expresssn: function(e){
		var the=this
		var value = e.detail.value
		var pagedata=the.data.pagedata
		//console.log(value);
		pagedata.post.expresssn=value;
		the.setData({
			'pagedata.post':pagedata.post,
		});
	},
	bindgroupsChange: function(e){
		var the=this
		var index = e.detail.value
		var pagedata=the.data.pagedata
		index=index?index:1;
		pagedata.post.express=pagedata.express[index].express;
		pagedata.post.expressv=pagedata.express[index].id
		pagedata.post.expressindex=index
		//console.log(pagedata.post);
		the.setData({
				'pagedata.post':pagedata.post,
		});
	},
  	click:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util[data.func](data.params);
	},
	click_tab:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util.tab(data.params);
	},
	formSubmit:function(event){
		var data=event.detail.value;
		app.util.post('post',data);
	},
	post:function(){
		var the = this
		var pagedata = the.data.pagedata
		app.util.post('jy_weishop/store.manage.order/set/id='+pagedata.item.id,pagedata.kd)
	},
});